package com.zzyl.serve.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.domain.vo.NursingProjectVo;
import com.zzyl.serve.mapper.NursingProjectMapper;
import com.zzyl.serve.service.INursingProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 护理项目Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-14
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    
    @Autowired
    private StringRedisTemplate redisTemplate;
    
    /**
     * 查询护理项目
     *
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id) {
        return getById(id);
    }
    
    /**
     * 查询护理项目列表
     *
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject) {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }
    
    /**
     * 新增护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject) {
        //清除缓存
        clearCache();
        return save(nursingProject) ? 1 : 0;
    }
    
    /**
     * 修改护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject) {
        //清除缓存
        clearCache();
        return updateById(nursingProject) ? 1 : 0;
    }
    
    /**
     * 批量删除护理项目
     *
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids) {
        //清除缓存
        clearCache();
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }
    
    /**
     * 删除护理项目信息
     *
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id) {
        //清除缓存
        clearCache();
        return removeById(id) ? 1 : 0;
    }
    
    
    /**
     * 查询所有护理项目
     * 方式一: 自定义查询sql,在查询时就处理好对应字段逻辑
     * @return
     */
    @Override
    public List<NursingProjectVo> selectAll() {
        //1 从redis中查询
        String json = redisTemplate.opsForValue().get(CacheConstants.NURSING_PROJECT_LIST);
        
        //2 查到了，则直接返回
        if (json != null) {
            return JSON.parseArray(json, NursingProjectVo.class);
        }
        //3  没查到，则查询数据库,并将数据存入redis中
        List<NursingProjectVo> nursingProjectVos = nursingProjectMapper.selectAll();
        redisTemplate.opsForValue().set(CacheConstants.NURSING_PROJECT_LIST, JSON.toJSONString(nursingProjectVos));
        return nursingProjectVos;
    }
    
    /**
     * 删除缓存中的数据
     */
    private void clearCache() {
        redisTemplate.delete(CacheConstants.NURSING_PROJECT_LIST);
        System.out.println("缓存已清除");
    }
    
    /**
     * 查询所有护理项目
     * 方式二: 封装查询条件,在mapper层处理逻辑
     *
     * @return
     */
    //@Override
    //public List<NursingProjectVo> selectAll() {
    //    //1 直接查询所有数据
    //    List<NursingProject> list = nursingProjectMapper.selectList(null);
    //
    //    //2 对数据进行处理,将id 和 name 封装到vo中
    //    List<NursingProjectVo> voList =
    //            list.stream().filter(item -> item.getStatus() == 1).map(item -> {
    //                NursingProjectVo vo = new NursingProjectVo();
    //                vo.setValue(item.getId().toString());
    //                vo.setLabel(item.getName());
    //                return vo;
    //            }).collect(Collectors.toList());
    //    return voList;
    //}
}